#!/usr/bin/python

import os
import code
import getpass
import sys
import optparse
import getpass

## Application specific
SDK_DIR = '/usr/local/google_appengine'
APP_DIR = './'
APPID = 'canescalendar'

REMOTE_API_PATH = '/remote_api'

## Extra paths to be inserted into sys.path,
## including the SDK, it's libraries, your APPDIR, and APPDIR/lib
EXTRA_PATHS = [
	SDK_DIR,
    os.path.join(SDK_DIR, 'lib', 'antlr3'),
    os.path.join(SDK_DIR, 'lib', 'django_0_96'),
    os.path.join(SDK_DIR, 'lib', 'fancy_urllib'),
    os.path.join(SDK_DIR, 'lib', 'ipaddr'),
    os.path.join(SDK_DIR, 'lib', 'webob'),
    os.path.join(SDK_DIR, 'lib', 'yaml', 'lib'),
    os.path.join(SDK_DIR, 'lib', 'simplejson'),
    os.path.join(SDK_DIR, 'lib', 'graphy'),
	APP_DIR,
	os.path.join(APP_DIR, 'lib'),
]
sys.path = EXTRA_PATHS + sys.path

from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
from scrape import *

def attach(username, host=None):
	def auth_func():
		if host and host.startswith('localhost'):
			return ('foo', 'bar')
		else:
			return (username, getpass.getpass())
	remote_api_stub.ConfigureRemoteApi(APPID, REMOTE_API_PATH, auth_func, host)
	remote_api_stub.MaybeInvokeAuthentication()
	os.environ['SERVER_SOFTWARE'] = 'Development (remote_api)/1.0'

if __name__=="__main__":
    parser = optparse.OptionParser("usage: %prog [-l letter] [-d hostname:port] [-e email] [schedule, myum, rmp]")
    
    parser.add_option("-l", "--letter", dest="letter", default=None,
                      type="string", help="letter to scrape")
                                            
    parser.add_option("-s", "--semester", dest="semester", default="20121",
                        type="string", help="semester to scrape (only used for the schedule scraper)")

    parser.add_option("-u", "--username", dest="username", default="rbrown46@gmail.com",
                        type="string", help="google login")

    parser.add_option("-d", "--host", dest="host", default="localhost:8080",
                        type="string", help="appengine hostname:port")

    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("incorrect number of arguments")
    letter = options.letter
    scraper = args[0]
    
    attach(options.username, options.host)
    
    letter = letter.upper() if letter != None else None
    
    if scraper == "schedule":
        if letter != None:
            scrapeUMCourse(options.semester, letter)
        else:
            for letter in map(chr, range(65, 91)):
                scrapeUMCourse(semester, letter)
    elif scraper == "myum":
        username = raw_input("Username: ")
        password = getpass.getpass("Password: ")
        
        if letter != None:
            scrapeUMRatings(username, password, letter)
        else:
            for letter in map(chr, range(65, 91)):
                scrapeUMRatings(username, password, letter)
    elif scraper == "rmp":
        if letter != None:
            scrapeRMP(letter)
        else:
            for letter in map(chr, range(65, 91)):
                scrapeRMP(letter)        
    